
package kaizone.haining.chaobohui.activity;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.CBHApplication;
import kaizone.haining.chaobohui.args.AppArgs;
import kaizone.haining.chaobohui.fragment.CBHContentFragment;
import kaizone.haining.chaobohui.json.BannerDO;
import kaizone.haining.chaobohui.json.UserDO;
import kaizone.haining.chaobohui.json.UserRegister;
import kaizone.haining.chaobohui.login.UserControl;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.setup.XinLangWeiBo;
import kaizone.haining.chaobohui.util.BDLocationWorker;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.Locate;
import kaizone.haining.chaobohui.util.ImageCache.ImageCacheParams;
import kaizone.haining.chaobohui.widget.AdView;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.slidingmenu.lib.SlidingMenu;

public class CBHActivity extends BaseActivity implements DataControl.IDataUpdateListener {

    public static final String IMAGE_CACHE_DIR = "CBHThumbs";

    private CBHApplication mCbhApp;

    private FragmentManager mFragmentManager;

    private Fragment mContent;

    private ImageFetcher mImageFetcher;

    private DataControl mDataControl;

    public CBHActivity() {
        super(R.string.app_name);
        mFragmentManager = getSupportFragmentManager();
    }

    public void addIgnoredView(View v) {
        SlidingMenu slidingMenu = getSlidingMenu();
        if (slidingMenu != null) {
            slidingMenu.addIgnoredView(v);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCbhApp = (CBHApplication) getApplication();
        if (savedInstanceState != null)
            mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
        if (mContent == null)
            mContent = CBHContentFragment.newInstance(mFragmentManager);

        initImageFetcher();
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // setContentView(R.layout.content_frame);
        // mFragmentManager.beginTransaction().replace(R.id.content_frame,
        // mContent).commit();
        //
        // getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        showContent();
        initLocate();
        initDataCotrol();
        initUserRegister();
        // initAd();
        loadAdtime();
    }

    public void loadAdtime() {
        showAd();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                hideAd();
            }
        }, 3000);
    }

    public void showAd() {
        AdView adView = (AdView) findViewById(R.id.adpanel);
        adView.setVisibility(View.VISIBLE);
        adView.loadAdBig();
        adView.fullScreen(this);
    }

    public void hideAd() {
        AdView adView = (AdView) findViewById(R.id.adpanel);
        adView.setVisibility(View.GONE);
    }

    public void showContent() {
        setContentView(R.layout.content_frame);
        mFragmentManager.beginTransaction().replace(R.id.content_frame, mContent).commit();

        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    }

    private void initImageFetcher() {
        final DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        final int height = displayMetrics.heightPixels;
        final int width = displayMetrics.widthPixels;

        final int longest = (height > width ? height : width) / 2;

        ImageCacheParams cacheParams = new ImageCacheParams(this, IMAGE_CACHE_DIR);
        cacheParams.setMemCacheSizePercent(this, 0.25f);
        mImageFetcher = new ImageFetcher(this, longest);
        // mImageFetcher.setLoadingImage(R.drawable.empty_photo);
        mImageFetcher.setLoadingImage(null);
        mImageFetcher.addImageCache(mCbhApp, cacheParams);
    }

    private LocationClient mLocationClient = null;

    private void initLocate() {
        BDLocationWorker bdLocationWorker = mCbhApp.getBdLocationWorker();
        if (bdLocationWorker == null) {
            bdLocationWorker = new BDLocationWorker(this);
            mCbhApp.setBdLocationWorker(bdLocationWorker);
        }
        bdLocationWorker.control(this);
    }

    private void initDataCotrol() {
        mDataControl = new DataControl(this);
        mDataControl.addChangeListener(this);
    }

    private void initUserRegister() {
        final boolean isRead = AppArgs.isReadUserInfo(this);
        if (!isRead)
            return;
        mDataControl.exec(UserControl.URL_REGISTER_USER);
    }

    private void initAd() {
        mDataControl.exec(BannerDO.URL);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mFragmentManager.putFragment(outState, "mContent", mContent);
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        if (TextUtils.isEmpty(requeststring))
            return null;
        if (requeststring.equals(UserControl.URL_REGISTER_USER)) {
            UserDO register = UserDO.parseJson(datastring);
            if (register != null) {
                AppArgs.writeUserInfo(this, register.getData());
            }
            return register;
        }//
        else if (requeststring.equals(BannerDO.URL)) {
            BannerDO bannerdo = BannerDO.parseJson(datastring);
            AppArgs.sBannerDO = bannerdo;
            return bannerdo;
        }
        return null;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (TextUtils.isEmpty(requeststring))
            return;
        if (requeststring.equals(BannerDO.URL)) {
            showAd();
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    hideAd();
                }
            }, 3000);
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    public void switchContent(Fragment fragment) {
        mContent = fragment;
        mFragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
        getSlidingMenu().showContent();
    }

    public Fragment getContent() {
        return mContent;
    }

    public ImageFetcher getImageFetcher() {
        return mImageFetcher;
    }

    public static void come(Context context) {
        Intent intent = new Intent(context, CBHActivity.class);
        context.startActivity(intent);
    }

}
